﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.Xml.Linq;
using micsun;
using System.IO;
using micsun.Web;

namespace tmsWeb
{
    public class Global : micsun.Web.ApplicationBase
    {
        static Global()
        {
            SiteSecurity.Instance.AuthenticateFailed += new EventHandler(OnAuthenticateFailed);
        }
      
        public static string AppPath;

        protected override void Application_Start(Object sender, EventArgs e)
        {
           
            base.Application_Start(sender, e);
        }

        static void OnAuthenticateFailed(object sender, EventArgs e)
        {
            IHttpHandler handler = HttpContext.Current.Handler;
            Type[] types = handler.GetType().GetInterfaces();
            if (types != null)
            {
                foreach (Type type in types)
                {
                    if (type == typeof(IDialogPage))
                    {
                        return;
                    }
                }
            }
            Transfer.ClearStoredRedirectParameters();
            Transfer.AddRedirectParameters(Transfer.Param_ReturnUrl, HttpContext.Current.Request.Url.ToString());
            Transfer.RedirectToNamedPage("Logon", false);
        }

        protected void Application_Error(Object sender, EventArgs e)
        {
            Exception ex = Server.GetLastError();
            if (ex != null)
            {
                ExceptionLog.Write(ex.GetBaseException(), "User IP:" + Request.UserHostName);
                Server.ClearError();
                HttpContext.Current.Response.Redirect(GetUrlRoot() + "error.aspx?Log=False&err=" + HttpUtility.UrlEncode(ex.GetBaseException().Message), true);
            }
        }
        protected override void Session_Start(Object sender, EventArgs e)
        {
            if (AppPath == null)
                AppPath = Server.MapPath(Request.ApplicationPath);
            base.Session_Start(sender, e);
        }

        protected override void Session_End(object sender, EventArgs e)
        {
            Logon.RemoveUser(Session);
            base.Session_End(sender, e);
        }

        

        

        private void DeleteTempDownFiles()
        {
            string tempPath = Server.MapPath("UpDown/Down/");

            foreach (string tempFileName in Directory.GetFiles(tempPath))
            {
                File.Delete(tempFileName);
            }
        }
    }
}